import axios from 'axios'
import token from './token'

let base = 'http://localhost:27000';

axios.interceptors.request.use(
	config => {
		config.headers = {
			'Authorization': token.getToken()
		}
		return config;
	}, err => {
		return Promise.resolve(err);
	})
axios.interceptors.response.use(data => {
	return data;
}, err => {
	return Promise.resolve(err);
})



 const formRequest = (url, params) => {
	return axios({
		method: 'post',
		url: `${base}${url}`,
		data: params,
		transformRequest: [function(data) {
			let ret = ''
			for(let it in data) {
				ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
			}
			return ret
		}],
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	});
}
const postRequest = (url, params) => {
 return axios({
	 method: 'post',
	 url: `${base}${url}`,
	 data: params||{},
	 headers: {
		 'Content-Type': 'application/json'
	 }
 });
}
 const uploadRequest = (url, params) => {
	return axios({
		method: 'post',
		url: `${base}${url}`,
		data: params,
		headers: {
			'Content-Type': 'multipart/form-data'
		}
	});
}
 const putRequest = (url, params) => {
	return axios({
		method: 'put',
		url: `${base}${url}`,
		data: params,
		transformRequest: [function(data) {
			let ret = ''
			for(let it in data) {
				ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
			}
			return ret
		}],
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	});
}
 const deleteRequest = (url) => {
	return axios({
		method: 'delete',
		url: `${base}${url}`
	});
}
 const getRequest = (url) => {
	return axios({
		method: 'get',
		url: `${base}${url}`
	});
}

export default{
	getRequest,deleteRequest,putRequest,uploadRequest,formRequest,postRequest
}
